from fastapi import APIRouter
from APP.app.tableparse.services.excel_base_service import ExcelBaseService
from APP.app.tableparse.services.excel_user_service import ExcelUserService
from APP.config.log_conf import log
from APP.common.respones.response_schema import response_base

excel_parse_router = APIRouter()

@excel_parse_router.get('/excel_parse', summary='解析Excel文件', description='解析Excel文件，返回解析结果')
async def excel_parse():
    
    # 从minio获取文件名称并验证文件白名单、文件存在性
    file_path = await ExcelBaseService.validate_file('text.xlsx') 

    #根据文件名称验证其sheet表是否白名单

    # 动态表头解析器
    column_map = await ExcelBaseService.dynamic_column_mapping(file_path, 'Sheet1')

    result_data_rows = await ExcelBaseService.parse_excel(file_path, 'Sheet1', column_map)    
    
    t = await ExcelUserService.create_excel_user(result_data_rows)
    log.info(f'解析结果:{len(t)}条数据已成功插入数据库')


    # return f'成功解析Excel文件{len(t)}条数据已成功插入数据库'
    return response_base.fast_success(data=f'成功解析Excel文件{len(t)}条数据已成功插入数据库')


